package service

import (
	"im/mapper"
	"im/model"
	"im/model/request"
	"im/model/response"

	"github.com/gin-gonic/gin"
)

// "goweb/web/global"

type ChatGroupMemberService struct{}

// 添加群成员
func (gma *ChatGroupMemberService) AddMember(c *gin.Context) {
	var chatGroupMemberReq request.ChatGroupMemberReq
	if err := c.ShouldBind(&chatGroupMemberReq); err != nil {
		// global.LOG.Error(err.Error())
		response.FailWithMessage("请求体解析失败", c)
		return
	}

	// Todo: 校验参数
	if u, err := mapper.AddMember(chatGroupMemberReq); err != nil {
		// global.LOG.Error(err.Error())
		response.FailWithMessage("新增群成员失败", c)
	} else {
		response.Result(200, u, "操作成功", c)
	}
	c.Abort()
}

// 删除群信息
func (gma *ChatGroupMemberService) DelMember(c *gin.Context) {
	var chatGroupReq request.ChatGroupMemberReq
	if err := c.ShouldBind(&chatGroupReq); err != nil {
		// global.LOG.Error(err.Error())
		response.FailWithMessage("请求体解析失败", c)
		return
	}

	// Todo: 校验参数
	if u, err := mapper.DelMember(chatGroupReq); err != nil {
		// global.LOG.Error(err.Error())
		response.FailWithMessage("删除群成员失败", c)
	} else {
		response.Result(200, u, "操作成功", c)
	}
	c.Abort()
}

// 群成员列表
func (gma *ChatGroupMemberService) ListMember(c *gin.Context) {
	var chatGroupReq request.ChatGroupMemberReq
	if err := c.ShouldBind(&chatGroupReq); err != nil {
		// global.LOG.Error(err.Error())
		response.FailWithMessage("请求体解析失败", c)
		c.Abort()
	}

	// Todo: 校验参数
	if u, err := mapper.ListMember(chatGroupReq); err != nil {
		// global.LOG.Error(err.Error())
		response.FailWithMessage("获取群成员列表失败", c)
	} else {
		response.Result(200, u, "操作成功", c)
	}
	c.Abort()
}

func (gma *ChatGroupMemberService) GetGroupMembers(chatGroupReq request.ChatGroupMemberReq) ([]model.GroupMember, error) {
	return mapper.ListMember(chatGroupReq)
}
